﻿using System.Collections.Generic;
using System.Windows.Forms;
using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.FormBase.PropertyGrid;
using WindowsForms.AOnline.WindowsForm.Resources;
using DevExpress.XtraEditors;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraAccountDetail : FrmXtraBaseTreeDetail, IAccountView, IAccountsView
    {
        private readonly AccountPresenter _accountPresenter;
        private readonly AccountsPresenter _accountsPresenter;


        /// <summary>
        /// Initializes a new instance of the <see cref="FrmXtraAccountDetail"/> class.
        /// </summary>
        public FrmXtraAccountDetail()
        {
            InitializeComponent();
            _accountPresenter = new AccountPresenter(this);
            _accountsPresenter = new AccountsPresenter(this);

        }

        #region IAccountsView Members

        /// <summary>
        /// Sets the accounts.
        /// </summary>
        /// <value>The accounts.</value>
        public IList<AccountModel> Accounts
        {
            set
            {
                grdLockUpParentID.Properties.DataSource = value;
                grdLockUpParentID.Properties.ForceInitialize();
                grdLockUpParentID.Properties.PopulateColumns();
                var treeColumnsCollection = new List<XtraColumn>
                                            {
                                                new XtraColumn
                                                {
                                                    ColumnName = "AccountId", ColumnCaption = "", ColumnVisible = false
                                                },
                                                  new XtraColumn
                                                {
                                                    ColumnName = "UnitId", ColumnCaption = "", ColumnVisible = false
                                                },
                                                  new XtraColumn
                                                {
                                                    ColumnName = "AccountCode", ColumnPosition = 1, ColumnCaption = "Mã tài khoản", ColumnVisible = true 
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "AccountName", ColumnPosition = 2, ColumnCaption = "Tên tài khoản", ColumnVisible = true 
                                                },
                                                
                                                 new XtraColumn
                                                {
                                                    ColumnName = "ParentId", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "Grade", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsDetail", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "Description", ColumnCaption = "", ColumnVisible = false
                                                },
                                             
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsCustomer", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsVendor", ColumnCaption = "", ColumnVisible = false
                                                },
                                                
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsInventoryItem", ColumnCaption = "", ColumnVisible = false
                                                },
                                                  new XtraColumn
                                                {
                                                    ColumnName = "IsFixedAsset", ColumnCaption = "", ColumnVisible = false
                                                },                                              
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsSystem", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsTax", ColumnCaption = "", ColumnVisible = false
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "IsActive",   ColumnPosition = 3,ColumnCaption = "Được sử dụng",  ColumnVisible = false                                            
                                                    
                                                }
                                            };

                foreach (var column in treeColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        grdLockUpParentID.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLockUpParentID.Properties.SortColumnIndex = column.ColumnPosition;
                    }
                    else grdLockUpParentID.Properties.Columns[column.ColumnName].Visible = false;
                }
                grdLockUpParentID.Properties.DisplayMember = "AccountCode";
                grdLockUpParentID.Properties.ValueMember = KeyFieldName;
            }
        }

        #endregion
        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            if (KeyValue != null)
            {
                _accountPresenter.Display(KeyValue);
                _accountsPresenter.DisplayForComboTree(int.Parse(KeyValue));
                grdLockUpParentID.Enabled = false;
            }
            else
            {
             //   _accountsPresenter.Display(cu);
                _accountsPresenter.GetAll();
            }
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {
            txtAccountCode.Focus();
            if ((ActionMode == ActionModeEnum.Edit) && HasChildren)
                grdLockUpParentID.Properties.ReadOnly = true;
            else
                grdLockUpParentID.Properties.ReadOnly = false;
        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected override bool ValidData()
        {
            if (string.IsNullOrEmpty(AccountCode))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResAccountCode"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAccountCode.Focus();
                return false;
            }
            //var listAccount = _accountsPresenter.GetAccountsActive();
            //foreach (var accountModel in listAccount)
            //{
            //    // option Edit
            //    if (AccountId > 0)
            //    {
            //        if (accountModel.AccountId != AccountId)
            //        {
            //            if (accountModel.AccountCode == AccountCode)
            //            {
            //                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResCheckAccountsCode"),
            //                                    ResourceHelper.GetResourceValueByName("ResDetailContent"),
            //                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            //                txtAccountCode.Focus();
            //                return false;
            //            }
            //        }
            //    }
            //    // option Add New
            //    else
            //    {
            //        if (accountModel.AccountCode == AccountCode)
            //        {
            //            XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResCheckAccountsCode"),
            //                                ResourceHelper.GetResourceValueByName("ResDetailContent"),
            //                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            //            txtAccountCode.Focus();
            //            return false;
            //        }
            //    }

            //}
            // Kiểm tra khi có tài khoản tiết con thì mã của Cha chứa mã con
            if (!AccountCode.Contains(grdLockUpParentID.Text))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResContainCodesParentError"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAccountCode.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(AccountName))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResAccountName"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAccountName.Focus();
                return false;
            }
          
            if (AccountCode == grdLockUpParentID.Text)
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResCodeSameAsParentError"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                grdLockUpParentID.Focus();
                return false;
            }

            return true;
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData()
        {
            return _accountPresenter.Save();
        }

        #region IAccountView Members

        /// <summary>
        /// Gets or sets the account identifier.
        /// </summary>
        /// <value>The account identifier.</value>
        public int AccountId { get; set; }

      

        /// <summary>
        /// Gets or sets the account code.
        /// </summary>
        /// <value>The account code.</value>
        public string AccountCode
        {
            get { return txtAccountCode.Text; }
            set { txtAccountCode.Text = value; }
        }

        /// <summary>
        /// Gets or sets the name of the account.
        /// </summary>
        /// <value>The name of the account.</value>
        public string AccountName
        {
            get { return txtAccountName.Text; }
            set { txtAccountName.Text = value; }
        }

        /// <summary>
        /// Gets or sets the parent identifier.
        /// </summary>
        /// <value>The parent identifier.</value>
        public int? ParentId
        {
            get
            {
                if (grdLockUpParentID.EditValue == null) return null;
                return (int?)grdLockUpParentID.GetColumnValue(KeyFieldName);
            }
            set
            {
                grdLockUpParentID.EditValue = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is system.
        /// </summary>
        /// <value><c>true</c> if this instance is system; otherwise, <c>false</c>.</value>
        public bool IsSystem { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is active.
        /// </summary>
        /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
        public bool IsActive
        {
            get { return chkIsActive1.Checked; }
            set { chkIsActive1.Checked = value; }
        }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return memoDescription.Text; }
            set { memoDescription.Text = value; }
        }

       
        /// <summary>
        /// Gets or sets the grade.
        /// </summary>
        /// <value>The grade.</value>
        public int Grade
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is detail.
        /// </summary>
        /// <value><c>true</c> if this instance is detail; otherwise, <c>false</c>.</value>
        public bool IsDetail
        {
            get;
            set;
        }

        
        /// <summary>
        /// Gets or sets a value indicating whether this instance is customer.
        /// </summary>
        /// <value><c>true</c> if this instance is customer; otherwise, <c>false</c>.</value>
        public bool IsCustomer
        {
            get { return chkIsCustomer.Checked; }
            set { chkIsCustomer.Checked = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is vendor.
        /// </summary>
        /// <value><c>true</c> if this instance is vendor; otherwise, <c>false</c>.</value>
        public bool IsVendor
        {
            get { return chkIsVendor.Checked; }
            set { chkIsVendor.Checked = value; }
        }

       

        /// <summary>
        /// Gets or sets a value indicating whether this instance is inventory item.
        /// </summary>
        /// <value><c>true</c> if this instance is inventory item; otherwise, <c>false</c>.</value>
        public bool IsInventoryItem
        {
            get { return chkIsInventoryItem.Checked; }
            set { chkIsInventoryItem.Checked = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is fixedasset.
        /// </summary>
        /// <value><c>true</c> if this instance is fixedasset; otherwise, <c>false</c>.</value>
        public bool IsFixedAsset
        {
            get { return chkIsFixedasset.Checked; }
            set { chkIsFixedasset.Checked = value; }
        }

        #endregion

        #region IAccountView Members


        /// <summary>
        /// Gets or sets a value indicating whether this instance is capital allocate.
        /// </summary>
        /// <value><c>true</c> if this instance is capital allocate; otherwise, <c>false</c>.</value>
        public bool IsTax
        {
            get { return chkIsTax.Checked; }
            set { chkIsTax.Checked = value; }
        }

        #endregion

        

       

        /// <summary>
        /// Handles the KeyDown event of the grdLockUpParentID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
        private void grdLockUpParentID_KeyDown(object sender, KeyEventArgs e)
        {
            if (grdLockUpParentID.SelectionLength == grdLockUpParentID.Text.Length && (e.KeyData == Keys.Back || e.KeyData == Keys.Delete))
            {
                grdLockUpParentID.EditValue = null;
                e.Handled = true;
            }
        }

   
        /// <summary>
        /// Handles the Load event of the FrmXtraAccountDetail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmXtraAccountDetail_Load(object sender, System.EventArgs e)
        {

        }


        public long UnitId
        {
            get;

            set;
          
        }




        public int BalanceSide
        {
            get { return cboBalance.SelectedIndex; }
            set { cboBalance.SelectedIndex = value; }
        }
    }
}